<template>
	<view>
		<view class="sc-wrap">
			<uni-segmented-control :current="curIndex" :values="tabs" styleType="button" @clickItem="onTabChange" />
		</view>
		<view class="list-wrap">
			<view class="card-bg item" v-for="(item,index) in list_data" :key="item.id" @click="toDetail(item.id)">
				<view class="left">
					<text class="time">{{formatTime(item)}}</text>
					<text class="name">{{item.intervieweeName}} {{item.intervieweePhone}}</text>
				</view>
				<text class="right">{{getStateName(item)}}</text>
				<image src="../../static/ic_arrow_r.png" class="arrow-r"></image>
			</view>
			<uni-load-more status="loading" v-show="isLoading" />
		</view>
		<xw-empty v-if="!list_data.length" v-on:refresh="onRefressh"></xw-empty>
		<uni-fab @fabClick="toScan" :pattern="pattern" horizontal="left"></uni-fab>
	</view>
</template>

<script>
	import cfg from '../../utils/config.js'
	export default {
		data() {
			return {
				tabs: ['待访问', '已到访', '未到达'],
				states: [2, 3, 4],
				curIndex: 0,

				pattern: {
					icon: 'scan'
				},
				list_data: [],
				page: 1,
				isLoading: false
			}
		},
		onPullDownRefresh() {
			this.onRefressh()
			setTimeout(function() {
				uni.stopPullDownRefresh()
			}, 2000)
		},
		onReachBottom() {
			this.page++;
			this.isLoading = true
			this.getList()
		},
		onShow() {
			uni.hideHomeButton()
			this.onRefressh()
		},
		methods: {
			onTabChange(e) {
				if (this.curIndex !== e.currentIndex) {
					this.curIndex = e.currentIndex
					this.onRefressh()
				}
			},
			getStateName(item) {
				if(!item){
					return
				}
				return cfg.getStateName(item.reachStatus)
			},
			formatTime(item) {
				if(!item){
					return
				}
				return cfg.timestampToTime(item.reservationReachTime)
			},
			toDetail(id) {
				uni.navigateTo({
					url: '/pages2/recept/detail?id=' + id
				})
			},
			toScan() {
				uni.scanCode({
					success: (res) => {
						if (res.result) {
							this.arrive(res.result)
						}
					}
				})
			},
			onRefressh() {
				this.page = 1
				this.list_data = [];
				this.getList()
			},
			getList() {
				uni.request({
					url: cfg.host + 'cp/reservation/page-query',
					method: 'POST',
					data: {
						'pageNo': this.page,
						'reachStatus': this.states[this.curIndex]
					},
					success: (res) => {
						if (res.data.code == 0) {
							let list = res.data.data.list || []
							if (this.page == 1) {
								this.list_data = list
							} else {
								this.list_data = this.list_data.concat(list)
							}
						}
					},
					complete: () => {
						this.isLoading = false
					}
				})
			},
			arrive(id) {
				uni.request({
					url: cfg.host + 'cp/reservation/visit/' + id,
					method: 'POST',
					success: (res) => {
						if (res.data.code == 0) {
							uni.showToast({
								icon: 'success',
								title: '访客已到达'
							})
							setTimeout(() => {
								this.toDetail(id)
							}, 1000)
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.sc-wrap {
		position: sticky;
		top: 0;
		z-index: 10;
		background-color: #f5f5f5;
		box-sizing: border-box;
		padding: 20rpx 60rpx;
	}

	.list-wrap {
		position: relative;
		width: 100%;
		display: flex;
		flex-direction: column;
	}

	.item {
		display: flex;
		flex-direction: row;
		align-items: center;

		.left {
			display: flex;
			flex-direction: column;
			flex: 1;

			.time {
				font-size: 30rpx;
			}

			.name {
				margin-top: 20rpx;
			}
		}

		.right {
			font-size: 28rpx;
			margin-right: 10rpx;
			font-size: 26rpx;
		}
	}
</style>